package com.example.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;


public class MovieListPage extends InternalPage {

    @FindBy(xpath = "//a[contains(@href,'go=add')]")
    public WebElement addMovieLink;

    @FindBy(xpath = "//a[contains(@href,'go=imdbupdate')]")
    public WebElement updateLink;

    @FindBy(xpath = "//a[contains(@href,'go=export')]")
    public WebElement exportLink;

    @FindBy(id = "results")
    public WebElement results;

//    @FindBy(className = "movie_box")
//    private List<WebElement> movieLinks;

    public void openMoviePageByTitle(String title) {
        this.getMovieLinkByTitle(title).click();
    }

    public WebElement getMovieLinkByTitle(String title) {
        return getMovieBy(By.xpath("//div[text()='" + title + "']"));
    }

    public WebElement getMovieById(String id) {
        return getMovieBy(By.id("movie_" + id));
    }

    private WebElement getMovieBy(By by) {
        try {
            return results.findElement(by);
        } catch (Exception e) {
            return null;
        }
    }

    public MovieAddPage goToMovieAddPage(){
        addMovieLink.click();
        return MyPageFactory.getPage(driver, MovieAddPage.class);
    }

}
